package com.freesky.common.utils.reponse;

import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.exception.MyException;

public class JSONResponseDiretor {
    /**
     * 创建成功返回对象
     * @param data
     * @return
     */
    public static JSONResponse buildSuccessJSONResponse(Object data) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(200).msg("success").object(data).build();
    }

    public static JSONResponse buildSuccessJSONResponse(ResponseEnum enumResponse, Object data) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(enumResponse.getCode()).msg(enumResponse.getMsg()).object(data).build();
    }

    /**
     * 创建携带异常返回对象
     * @param e
     * @return
     */
    public static JSONResponse buildErrorJSONResponseWithException(Exception e) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(500).msg("error").object(e).build();
    }

    /**
     * 创建失败返回对象
     * @param data
     * @return
     */
    public static JSONResponse buildErrorJSONResponse(Object data) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(-1).msg("error").object(data).build();
    }

    /**
     * 创建失败返回对象
     * @param data
     * @return
     */
    public static JSONResponse buildErrorJSONResponse(ResponseEnum enumResponse, Object data) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(enumResponse.getCode()).msg(enumResponse.getMsg()).object(data).build();
    }

    /**
     * 创建全局异常统一处理返回对象
     * @param e 抛出的异常
     * @return
     */
    public static JSONResponse buildMyExceptionJSONResponse(MyException e) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(e.getResponse().getCode()).msg(e.getResponse().getMsg()).object(e.getResponse().getData()).build();
    }

    /**
     * 创建调用服务失败返回对象
     * @param msg 失败消息
     * @param data 可携带返回数据
     * @return
     */
    public static JSONResponse buildBadServiceJSONResponse(String msg, Object data) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(-1).msg(msg).object(data).build();
    }

    /**
     * 创建缺乏权限返回对象
     * @param data
     * @return
     */
    public static JSONResponse buildLackPermJSONResponse(Object data) {
        JSONResponseBuilder builder = new ConcreteJSONResponseBuilder();
        return builder.code(ResponseEnum.LACK_PERM.getCode()).msg(ResponseEnum.LACK_PERM.getMsg()).object(data).build();
    }
}
